#ifndef _PROTOCOL_HTTP_H_
#define _PROTOCOL_HTTP_H_

#include "byte.h"

enum
{
    HTTP_SIGN_ROOT = 0,
    HTTP_SIGN_LINE,
    HTTP_SIGN_HEADER,
    HTTP_SIGN_BODY
};

class CHttpBuffer : public CByteBuffer
{
    CHttpBuffer(const CHttpBuffer &r);

    CHttpBuffer& operator= (const CHttpBuffer& r);

public:

    CHttpBuffer(const char* name = "");

    CHttpBuffer(const char* inbuf, ushort inlen, const char* name = "");

    virtual ~CHttpBuffer(){}

    virtual int ToBlock(CByteBlock* block);

private:

    bool FindLabel(const char* &begin, ushort len, const char* &label, ushort &labelsize);

    bool ParseLabel(CByteBlock* block, const char* label, ushort labelsize);
};

class CHttpBlock : public CByteBlock
{
    template<typename T> friend CByteBlock* AddChild(CByteBlock* block, uchar sign);

    CHttpBlock(const CHttpBlock &r);

    CHttpBlock& operator= (const CHttpBlock& r);

    CHttpBlock(uchar sign, CByteBlock* parent);

public:

    CHttpBlock(const char* blockname = "");

    virtual ~CHttpBlock(){}

    virtual int ToBuffer(CByteBuffer* buffer);
};

struct HttpLine
{
    HttpLine():_version("HTTP/1.1"){}

    string _method;
    string _url;
    string _version;//HTTP/1.*
};

bool de_http_line(CByteBlock& block, HttpLine& httpline);

bool en_http_line(CByteBlock& block, HttpLine& httpline);

bool en_http_header(CByteBlock& block, string name, string value);

bool en_http_body(CByteBlock& block, string body);

#endif //_PROTOCOL_HTTP_H_
